<?php 
	if(isset($_REQUEST['xml']) && ($_REQUEST['xml'] == '1')){
		$xml=1;
	}else{$xml=0;}

	try{
		$db = new PDO('sqlite:../../projecto1.db');
	}
	catch(PDOException $err){
		if($xml==0){
			$error['Result'] = array('Login'=>'DatabaseError');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('Register');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('DatabaseError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	if(isset($_REQUEST['username']) && ($_REQUEST['username'] != "")){
		$login=$_REQUEST['username'];
	}else{$login="";}
	
	$pass=sha1($_REQUEST['pass']);
	
	if($login == ""){
		session_start();
		if(!session_is_registered(username)){
			session_destroy();
			if($xml==0){
				$error['Result'] = array('Username'=>"No session started");
				echo json_encode($error);
				die;
			}else{
				$doc = new DomDocument('1.0');
				$doc->formatOutput = TRUE;
				// create root node
				$root = $doc->createElement('Result');
				$root = $doc->appendChild($root);
				$attr = $doc->createAttribute("xmlns:xsi");
				$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
				$root->appendChild($attr);
				$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
				$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
				$root->appendChild($attr);
				//childs of root
				$error = $doc->createElement('Username');
				$error= $root->appendChild($error);
				$value = $doc->createTextNode("No session started");
				$value = $error->appendChild($value);
				echo $doc->saveXML();
				die;
			}
		}else{
			if($xml==0){
				$error['Result'] = array('Username'=>$_SESSION['username']);
				echo json_encode($error);
				die;
			}else{
				$doc = new DomDocument('1.0');
				$doc->formatOutput = TRUE;
				// create root node
				$root = $doc->createElement('Result');
				$root = $doc->appendChild($root);
				$attr = $doc->createAttribute("xmlns:xsi");
				$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
				$root->appendChild($attr);
				$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
				$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
				$root->appendChild($attr);
				//childs of root
				$error = $doc->createElement('Username');
				$error= $root->appendChild($error);
				$value = $doc->createTextNode($_SESSION['username']);
				$value = $error->appendChild($value);
				echo $doc->saveXML();
				die;
			}
		}
	}
	
	$bool=0;
	$result1 = $db->query('SELECT * FROM User');
	foreach( $result1 as $row) {
		if($row['userName'] == $login){
			if($row['userPassword'] == $pass){
				$bool=1;
			}
		}
	}
	if($bool == 1){
		session_start(); 
		$_SESSION['username'] = $login;
		//header("refresh:2; url=../links/user.php");
		if($xml==0){
			$error['Result'] = array('Login'=>'Success');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('Login');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('Success');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	else{
		if($xml==0){
			$error['Result'] = array('Login'=>'wrongUsernameOrPass');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('Login');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('wrongUsernameOrPass');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	
?>